package cn.exrick.xboot.base.serviceimpl

import cn.exrick.xboot.base.dto.BreakRolesRecordDto
import cn.exrick.xboot.base.entity.BreakRolesRecord
import cn.exrick.xboot.base.entity.HiddenDangerRecord
import cn.exrick.xboot.base.mapper.HiddenDangerRecordMapper
import cn.exrick.xboot.base.service.IEnterpriseUserService
import cn.exrick.xboot.base.service.IHiddenDangerRecordService
import cn.exrick.xboot.base.vo.HiddenDangerRecordDetailVo
import cn.exrick.xboot.base.vo.HiddenDangerRecordListVo
import cn.exrick.xboot.core.common.utils.PageUtil
import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.utils.SecurityUtil
import cn.exrick.xboot.core.common.utils.ToolUtil
import cn.exrick.xboot.core.common.vo.PageVo
import cn.exrick.xboot.core.common.vo.Result
import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.google.common.collect.Maps
import lombok.extern.slf4j.Slf4j
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * 隐患记录接口实现
 * @author Fei
 */
@Slf4j
@Service
@Transactional
open class IHiddenDangerRecordServiceImpl : ServiceImpl<HiddenDangerRecordMapper?, HiddenDangerRecord?>(), IHiddenDangerRecordService {


    @Autowired
    private lateinit var securityUtil: SecurityUtil

    @Autowired
    private lateinit var iEnterpriseUserService: IEnterpriseUserService


    override fun createOrUpdateHiddenDangerRecord(hiddenDangerRecord: HiddenDangerRecord): Result<Any?>? {

        return try {

            saveOrUpdate(hiddenDangerRecord)
            ResultUtil.success("操作成功")
        } catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun removeHiddenDangerRecord(ids: Array<out String>): Result<Any?>? {

        return try {

            removeByIds(ids.toList())
            ResultUtil.success("操作成功！")
        } catch (e: Exception) {

            ResultUtil.error(e.message)
        }
    }

    override fun getHiddenDangerRecordList(breakRolesRecordDto: BreakRolesRecordDto, pageVo: PageVo): Result<MutableMap<String, Any?>>? {

        val hiddenDangerRecordWrapper = KtQueryWrapper(HiddenDangerRecord::class.java)
                .eq(HiddenDangerRecord::createBy, securityUtil.currUser.id)
                .orderByDesc(BreakRolesRecord::createTime)
        if (ToolUtil.isNotEmpty(breakRolesRecordDto.enterpriseUserId)) hiddenDangerRecordWrapper.eq(HiddenDangerRecord::enterpriseUserId,
                breakRolesRecordDto.enterpriseUserId)

        return ToolUtil.setListToNul(list(hiddenDangerRecordWrapper))?.run {

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { getHiddenDangerRecordListVo(it?.id) })

            ResultUtil.data(result, "获取隐患事故数据成功！")
        } ?: ResultUtil.error(201, "暂无数据！")
    }

    /**
     * 获取封装类
     */
    private fun getHiddenDangerRecordListVo(id: String?): HiddenDangerRecordListVo? {

        return getById(id)?.run {

            HiddenDangerRecordListVo().apply {

                enterpriseName = iEnterpriseUserService.getEnterpriseNameById(enterpriseUserId)
                ToolUtil.copyProperties(this@run, this)
            }
        }
    }

    override fun getHiddenDangerRecordDetail(id: String?): Result<HiddenDangerRecordDetailVo>? {

        return getById(id)?.run {

            ResultUtil.data(HiddenDangerRecordDetailVo().apply {

                hiddenDangerPicList = hiddenDangerPics?.split(",")?.toMutableList()
                ToolUtil.copyProperties(this@run, this)
            }, "获取详情成功")
        } ?: ResultUtil.error(201, "不存在此数据！")
    }

    override fun getMyHiddenDangerRecord(pageVo: PageVo): Result<MutableMap<String, Any?>>? {

        return ToolUtil.setListToNul(list(KtQueryWrapper(HiddenDangerRecord::class.java).eq(HiddenDangerRecord::enterpriseUserId,
                iEnterpriseUserService.getEnterpriseIdByEmployeeUserId(securityUtil.currUser.id))
                .orderByDesc(BreakRolesRecord::createTime)))?.run {

            val result: MutableMap<String, Any?> = Maps.newHashMap()
            result["size"] = size
            result["content"] = PageUtil.listToPage(pageVo, mapNotNull { getHiddenDangerRecordListVo(it?.id) }.toMutableList())

            ResultUtil.data(result, "获取我的隐患记录成功")
        } ?: ResultUtil.error(201, "暂无数据！")
    }

}